package com.strange.mdl.module.mdl.controller.admin.sutras;

import com.strange.mdl.module.mdl.controller.admin.sutras.vo.sutras.MdlSutrasPageReqVO;
import com.strange.mdl.module.mdl.controller.admin.sutras.vo.sutras.MdlSutrasRespVO;
import com.strange.mdl.module.mdl.controller.admin.sutras.vo.sutras.MdlSutrasSaveReqVO;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.strange.mdl.framework.common.pojo.PageParam;
import com.strange.mdl.framework.common.pojo.PageResult;
import com.strange.mdl.framework.common.pojo.CommonResult;
import com.strange.mdl.framework.common.util.object.BeanUtils;
import static com.strange.mdl.framework.common.pojo.CommonResult.success;

import com.strange.mdl.framework.excel.core.util.ExcelUtils;

import com.strange.mdl.framework.apilog.core.annotation.ApiAccessLog;
import static com.strange.mdl.framework.apilog.core.enums.OperateTypeEnum.*;

import com.strange.mdl.module.mdl.dal.dataobject.sutras.MdlSutrasDO;
import com.strange.mdl.module.mdl.service.sutras.MdlSutrasService;

@Tag(name = "管理后台 - 经书")
@RestController
@RequestMapping("/mdl/sutras")
@Validated
public class MdlSutrasController {

    @Resource
    private MdlSutrasService sutrasService;

    @PostMapping("/create")
    @Operation(summary = "创建经书")
    @PreAuthorize("@ss.hasPermission('mdl:sutras:create')")
    public CommonResult<Long> createSutras(@Valid @RequestBody MdlSutrasSaveReqVO createReqVO) {
        return success(sutrasService.createSutras(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新经书")
    @PreAuthorize("@ss.hasPermission('mdl:sutras:update')")
    public CommonResult<Boolean> updateSutras(@Valid @RequestBody MdlSutrasSaveReqVO updateReqVO) {
        sutrasService.updateSutras(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除经书")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mdl:sutras:delete')")
    public CommonResult<Boolean> deleteSutras(@RequestParam("id") Long id) {
        sutrasService.deleteSutras(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除经书")
                @PreAuthorize("@ss.hasPermission('mdl:sutras:delete')")
    public CommonResult<Boolean> deleteSutrasList(@RequestParam("ids") List<Long> ids) {
        sutrasService.deleteSutrasListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得经书")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mdl:sutras:query')")
    public CommonResult<MdlSutrasRespVO> getSutras(@RequestParam("id") Long id) {
        MdlSutrasDO sutras = sutrasService.getSutras(id);
        return success(BeanUtils.toBean(sutras, MdlSutrasRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得经书分页")
    @PreAuthorize("@ss.hasPermission('mdl:sutras:query')")
    public CommonResult<PageResult<MdlSutrasRespVO>> getSutrasPage(@Valid MdlSutrasPageReqVO pageReqVO) {
        PageResult<MdlSutrasDO> pageResult = sutrasService.getSutrasPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MdlSutrasRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出经书 Excel")
    @PreAuthorize("@ss.hasPermission('mdl:sutras:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSutrasExcel(@Valid MdlSutrasPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MdlSutrasDO> list = sutrasService.getSutrasPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "经书.xls", "数据", MdlSutrasRespVO.class,
                        BeanUtils.toBean(list, MdlSutrasRespVO.class));
    }

}